// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import axios from 'axios'
import store from './vuex/store'
Vue.use(ElementUI);
Vue.use(Vuex)
Vue.use(VueRouter)
Vue.config.productionTip = false

// 每次跳转路由都把当前路由存储下来（放到sessionStorage里面）
router.beforeEach((to, from, next) => {
  console.log(`to.name>>>>>>>>>>>>>>>`, typeof to.name)
  // 如果不存在路由，则要跳转的为字符串undefined
  if (to.name == `undefined`) {
    console.log(`不存在`)
  }
  // 如果跳转的是router里面定义的路由就存下来 
  else {
    console.log(`set routerName`, to.name)
    window.sessionStorage.setItem(`routerName`, to.name)
  }

  next()
})

Vue.prototype.$http = request;
function request({
  method,
  url,
  params,
  customErr = false
}) {
  return new Promise((resolve, reject) => {
    axios({
      method: method,
      url: url,
      [method === "post" ?
        "data" :
        "params"
      ]: params
    }).then(response => {
      if (customErr) {
        resolve(response.data);
      } else {
        if (response.data.code === 0) {
          resolve(response.data.data);
        } else {
          alert('error')
        }
      }
    }, error => {
      alert('error');
      reject(error)
    }).catch(error => {
      alert('error');
      reject(error)
    });
  });
}
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
